﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.woodsoft.core;
using EQ1.Elements;
using com.woodsoft.core.Files;
using System.Xml;
using EQ1.Objects;

namespace EQ1.Controls {
  public class TabBox :Control, IUIElement<TabBox>{
    public TabBox ( UIDocument doc , string name ) : base( doc , name ) {
      this.ShowTabs = true;
      this.TabStyle = 0;
    }
    public TabBox ( UIDocument doc ) : this( doc , "TabBox" ) {
    }

    #region IUIElement<TabBox> Members

    public void Validate ( TabBox val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        TabBox el = new TabBox( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }

    public void Copy ( TabBox val ) {
      base.Copy( val );

      this.ShowTabs = val.ShowTabs;
      this.TabStyle = val.TabStyle;

      if( !string.IsNullOrEmpty( val.TabBorderTemplate ) )
        this.TabBorderTemplate = val.TabBorderTemplate;
      if( val.TabWidth != null )
        this.TabWidth = val.TabWidth;
      if( !string.IsNullOrEmpty( val.PageBorderTemplate ) )
        this.PageBorderTemplate = val.PageBorderTemplate;
      if( val.Pages != null )
        this.Pages = val.Pages;
    }

    #endregion

    public bool ShowTabs {
      get {
        return bool.Parse( this.GetElement( "ShowTabs" ).InnerText );
      }
      set {
        this.Validate( "ShowTabs" , value.ToString() );
      }
    }
    public int TabStyle {
      get {
        return int.Parse( this.GetElement( "TabStyle" ).InnerText );
      }
      set {
        this.Validate( "TabStyle" , value.ToString() );
      }
    }

    public string TabBorderTemplate {
      get {
        return this.GetElement( "TabBorderTemplate" ).InnerText;
      }
      set {
        this.Validate( "TabBorderTemplate" , value );
      }
    }
    public int? TabWidth {
      get {
        return int.Parse( this.GetElement( "TabWidth" ).InnerText );
      }
      set {
        this.Validate( "TabWidth" , value.ToString() );
      }
    }
    public string PageBorderTemplate {
      get {
        return this.GetElement( "PageBorderTemplate" ).InnerText;
      }
      set {
        this.Validate( "PageBorderTemplate" , value );
      }
    }
    public XmlNodeList Pages {
      get {
        return this.GetElementsByTagName( "Pages" );
      }
      set {
        foreach( XmlNode nd in value ) {
          if( nd.LocalName == "Pages" )
            this.AddPage( nd );
        }
      }
    }

    public void AddPage ( XmlNode val ) {
      if( !string.IsNullOrEmpty( val.InnerText ) ) {
        for( int i = this.Pages.Count - 1 ; i >= 0 ; i-- ) {
          if( val.InnerText == this.Pages[i].InnerText )
            return;
        }

        if( val.LocalName == "Pages" )
          this.AppendChild( val );
        else
          this.AddElement( "Pages" , val.InnerText );
      }
    }
  }
}
